<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Hd_invitation_code extends PC_controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Hdinvitationcustomermodel','m_customer');
    }

    public function index(){
        $data = parent::index();

        global $g_hd_invitation_gift_type;
        $data['game_gift'] = array4js($g_hd_invitation_gift_type);
        $data['includeExcel'] = 1;

        $this->load->view('admin/hd_invitation_code',$data);
    }

    public function getCodeList() {
        $assist['where'] = getFilter($this->input->post(),Hdinvitationcustomermodel::$historySearchField);
        $assist['like'] = getFilter($this->input->post(),Hdinvitationcustomermodel::$historyLikeField);
        $assist['pagination'] = getPagination($this->input->post());

        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] == 2) {
            $assist['where']['user.user_id'] = $this->m_app->getCurrentUserId();
        }
        $assist['where']['hd_invitation_lottery_history.gift_status'] = STATUS_ACTIVE;
        $assist['where_sql'] = 'code IS NOT NULL';
        $assist['join_user'] = true;

        $assist['select'] = '
            hd_invitation_customer.customer_id,
            hd_invitation_customer.openid,
            user.user_name,
            user.user_code,
            hd_invitation_lottery_history.*
        ';
        list($list,$count) = $this->m_customer->getLotteryHistoryListSection($assist);
        // var_dump($this->db->last_query());

        $this->response->setSuccess();
        $this->response->setItems($list);
        $this->response->setTotalCount($count);
        $this->response->responseExit();
    }

    public function exportList() {
        @set_time_limit(0);
        @ini_set('memory_limit', '4048M');

        $info = $this->input->get();
        $assist['where'] = getFilter($this->input->get(),Hdinvitationcustomermodel::$historySearchField);
        $assist['like'] = getFilter($this->input->get(),Hdinvitationcustomermodel::$historyLikeField);
        $assist['sort'] = getSort($this->input->get(),Hdinvitationcustomermodel::$sortField);

        $this->load->library('zmexcel');
        $this->zmexcel->init();
        // $this->zmexcel->addSheet('广汽本田9月区域特色售后服务活动核销记录');
        $this->zmexcel->setTitle(array(
            array('content'=>'店铺编码'),
            array('content'=>'店铺名称'),
            array('content'=>'顾客ID'),
            array('content'=>'openid'),
            array('content'=>'类别'),
            array('content'=>'卡券号'),
            // array('content'=>'状态'),
            array('content'=>'姓名'),
            array('content'=>'车架号'),
            array('content'=>'创建时间'),
            array('content'=>'激活时间'),
            array('content'=>'核销来源'),
        ));

        $user = $this->session->userdata('user');
        // 4s店
        if($user['group_id'] == 2) {
            $assist['where']['user_id'] = $this->m_app->getCurrentUserId();
        }
        $assist['where']['hd_invitation_lottery_history.gift_status'] = STATUS_ACTIVE;
        $assist['where_sql'] = 'code IS NOT NULL';
        $assist['join_user'] = true;

        $assist['select'] = '
            hd_invitation_customer.customer_id,
            hd_invitation_customer.openid,
            user.user_name,
            user.user_code,
            hd_invitation_lottery_history.*
        ';
        list($list,$count) = $this->m_customer->getLotteryHistoryListSection($assist);

        if(count($list) > 30000) {
            die('导出数据太大，请筛选数据导出!');
        }

        global $g_hd_invitation_gift_type;

        foreach ($list as $key => $value) {
            $this->zmexcel->addRow(array(
                array('content' => $value['user_code']),
                array('content' => $value['user_name']),
                array('content' => $value['customer_id']),
                array('content' => $value['openid']),
                array('content' => id2name($value['gift_type'],$g_hd_invitation_gift_type)),
                array('content' => $value['code']),
                // array('content' => $value['gift_status'] == STATUS_ACTIVE ? '已领取' : '未领取'),
                array('content' => $value['customer_name']),
                array('content' => $value['customer_car_number']),
                array('content' => $value['lottery_time_time_create']),
                array('content' => $value['lottery_time_time_active']),
                array('content' => $value['validate_source']),
            ));
            unset($list[$key]);
        }

        // $this->excel->freezePanes(1,0);
        $this->zmexcel->generate('广汽本田9月区域特色售后服务活动核销记录_'.date('YmdHis').'.xls');
    }

}
/* End of file aa_game_history.php */
/* Location: ./application/controllers/aa_game_history.php */
